﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MyCadDll;
using MyChart;
using MyTool.FileOpt;

namespace cad2013
{
    public enum DingWeiFlag
    {
        dwInvalid = 0, dwName = 1, dwPeiXian1 = 2, dwPeiXian2 = 3, dwJianJu = 4
    }

    public partial class ReadCadDataForm : Form
    {
        private Point m_namePoint;
        private Point m_peiXian1;
        private Point m_peiXian2;
        private Point m_name2Point;

        private DingWeiFlag m_dingWeiStart = DingWeiFlag.dwInvalid;
        private DataTable m_dt;

        public ReadCadDataForm()
        {
            InitializeComponent();
        }

        public ReadCadDataForm(DataTable dt)
        {
            InitializeComponent();
            m_dt = dt;
            dataGridView1.DataSource = dt;
        }

        private void ReadJyFenXianPanbutton_Click(object sender, EventArgs e)
        {
            ReadJyFenXianPan();
        }

        //private bool isValidStr(FxpDzBoard board,)
        //{
        //    return false;
        //}

        private bool isContain(string[] strs, string str)
        {
            foreach (string s in strs)
            {
                if (str.Trim().Contains(s))
                    return true;
            }
            return false;
        }

        private bool isEndStr(string[] strs, string str)
        {
            foreach (string s in strs)
            {
                if (str.Trim().EndsWith(s))
                    return true;
            }
            return false;
        }

        private void ReadJyFenXianPan()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("./\\JyTestFilter.xml");
            XmlNode root = doc.DocumentElement;
            XmlNode node = root.SelectSingleNode("绝缘/绝缘测试筛选");
            string[] endStrs = node.Attributes["结尾"].Value.Split(new char[] { ' ' });
            string[] containtStrs = node.Attributes["包含"].Value.Split(new char[] { ' ' });
            string[] otherEndStrs = node.Attributes["有条件结尾"].Value.Split(new char[] { ' ' });

            IniFile inifile = new IniFile("./\\mycad.ini");
            inifile.Load();
            int n = inifile.GetInt("jy", "sum", 0);
            FxpChart fxp = new FxpChart(m_dt);
            string str1, str2;
            foreach (FxpDzBoard board in fxp.DzBoardList)
            {
                string x5 = board.FxpDzs[4].DzUsers[0].Trim();
                string x4 = board.FxpDzs[3].DzUsers[0].Trim();
                string x3 = board.FxpDzs[2].DzUsers[0].Trim();
                if (!string.IsNullOrEmpty(x5) && x5.EndsWith("-X5"))
                {
                    str1 = string.Format("{0,-30}{1,-30}", x4, board.FxpDzBoardId.ToString() + "-4");
                    str2 = string.Format("{0,-30}{1,-30}", x5, board.FxpDzBoardId.ToString() + "-5");
                    inifile.WriteString("jy", (++n).ToString(), str1);
                    inifile.WriteString("jy", (++n).ToString(), str2);
                }
                else if (!string.IsNullOrEmpty(x4) && x4.EndsWith("-X4"))
                {
                    str1 = string.Format("{0,-30}{1,-30}", x3, board.FxpDzBoardId.ToString() + "-3");
                    str2 = string.Format("{0,-30}{1,-30}", x4, board.FxpDzBoardId.ToString() + "-4");
                    inifile.WriteString("jy", (++n).ToString(), str1);
                    inifile.WriteString("jy", (++n).ToString(), str2);
                }

                foreach (PeiXianDz dz in board.FxpDzs)
                {
                    for (int i = 0; i < dz.DzUsers.Length; i++)
                    {
                        if (string.IsNullOrEmpty(dz.DzUsers[i])) continue;
                        //if ((dz.DzUsers[0].EndsWith("H") && !board.Style.Contains("信号")) ||
                        //    dz.DzUsers[0].EndsWith("-GF") || dz.DzUsers[0].EndsWith("-MF") || dz.DzUsers[0].EndsWith("-F") || dz.DzUsers[0].EndsWith("-FMH")
                        //    || dz.DzUsers[0].EndsWith("-LUH") || dz.DzUsers[0].EndsWith("-HBH") || dz.DzUsers[0].EndsWith("-HH")
                        //    || dz.DzUsers[0].EndsWith("-BAH") || dz.DzUsers[0].EndsWith("-DS1") || dz.DzUsers[0].EndsWith("-DS2")
                        //    || dz.DzUsers[0].EndsWith("-DS3") || dz.DzUsers[0].EndsWith("-QH") || dz.DzUsers[0].EndsWith("-YBH")
                        //    || dz.DzUsers[0].EndsWith("-LH") || dz.DzUsers[0].EndsWith("-UH") || dz.DzUsers[0].Contains("-GT")
                        //    || dz.DzUsers[0].EndsWith("-NBH")|| dz.DzUsers[0].Contains("-备"))
                        string strStyle = board.Style.Trim();
                        if (isEndStr(otherEndStrs, dz.DzUsers[i]) && (dz.DzUsers[i].EndsWith("G-H") == false) &&
                            (strStyle.Contains("信号")))
                        {
                            continue;
                        }
                        else if (isContain(containtStrs, dz.DzUsers[i]) || isEndStr(endStrs, dz.DzUsers[i]))
                        {
                            string str = string.Format("{0,-30}{1,-30}", dz.DzUsers[i], board.FxpDzBoardId.ToString() + "-" + dz.DzId.ToString());
                            inifile.WriteString("jy", (++n).ToString(), str);
                        }
                    }
                }
            }
            inifile.WriteInt("jy", "sum", n);
            inifile.Save();
            MessageBox.Show("ok");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int startRowIndex = 4;
            int dcNameColIndex = 1;
            int zhColIndex = 3;
            IniFile inifile = new IniFile("./\\dc.txt");
            inifile.Load();
            int n = inifile.GetInt("直流道岔", "sum", 0);

            for (int i = startRowIndex; i < m_dt.Rows.Count; i++)
            {
                string dcName = m_dt.Rows[i][dcNameColIndex].ToString().Trim();
                if (string.IsNullOrEmpty(dcName)) continue;
                string zhName, fxpName;
                zhName = m_dt.Rows[i][zhColIndex].ToString().Trim();
                fxpName = m_dt.Rows[i - 1][zhColIndex].ToString().Trim();
                zhName = zhName.Substring(0, zhName.LastIndexOf('-') - 2);
                fxpName = fxpName.Substring(0, fxpName.LastIndexOf('-'));
                inifile.WriteString("直流道岔", (++n).ToString(), dcName + "  " + fxpName + "  " + zhName);
            }
            inifile.WriteInt("直流道岔", "sum", n);
            inifile.Save();
            MessageBox.Show("ok");
        }

        private void ReadGdbutton_Click(object sender, EventArgs e)
        {
            int startRowIndex = 5;
            int gdNameColIndex = 1;
            int peiXianColIndex = 10;
            IniFile inifile = new IniFile("./\\gd.txt");
            inifile.Load();
            int n = inifile.GetInt("轨道", "sum", 0);

            for (int i = startRowIndex; i < m_dt.Rows.Count; i++)
            {
                string gdName = m_dt.Rows[i][gdNameColIndex].ToString().Trim();
                if (string.IsNullOrEmpty(gdName)) continue;
                string peiXian1, peiXian2;
                peiXian1 = m_dt.Rows[i][peiXianColIndex].ToString().Trim();
                peiXian2 = m_dt.Rows[i][peiXianColIndex + 1].ToString().Trim();

                inifile.WriteString("轨道", (++n).ToString(), gdName + "  " + peiXian1 + "  " + peiXian2);
            }
            inifile.WriteInt("轨道", "sum", n);
            inifile.Save();
            MessageBox.Show("ok");
        }

        private void Otherbut_Click(object sender, EventArgs e)
        {
            int startRowIndex = m_namePoint.Y;
            int gdNameColIndex = m_namePoint.X;
            int peiXian1Dx = m_peiXian1.X - m_namePoint.X;
            int peiXian1Dy = m_peiXian1.Y - m_namePoint.Y;
            int peiXian2Dx = m_peiXian2.X - m_namePoint.X;
            int peiXian2Dy = m_peiXian2.Y - m_namePoint.Y;
            int nameJianJuY = m_name2Point.Y - m_namePoint.Y;
            IniFile inifile = new IniFile("./\\other.txt");
            inifile.Load();
            int n = inifile.GetInt("其他", "sum", 0);

            for (int i = startRowIndex; i < m_dt.Rows.Count; i = i + nameJianJuY)
            {
                string gdName = m_dt.Rows[i][gdNameColIndex].ToString().Trim();
                if (string.IsNullOrEmpty(gdName)) continue;
                string peiXian1, peiXian2;
                peiXian1 = m_dt.Rows[i + peiXian1Dy][m_peiXian1.X].ToString().Trim();
                peiXian2 = m_dt.Rows[i + peiXian2Dy][m_peiXian2.X].ToString().Trim();

                inifile.WriteString("其他", (++n).ToString(), gdName + "  " + peiXian1 + "  " + peiXian2);
            }
            inifile.WriteInt("其他", "sum", n);
            inifile.Save();
            MessageBox.Show("ok");
        }

        private void Locationbut_Click(object sender, EventArgs e)
        {
            m_dingWeiStart = DingWeiFlag.dwName;
            MessageBox.Show("请依次点击设备 名称 配线1 配线2 下一个设备名称 单元格进行定位");
        }
    }
}